package com.ruoyi.web.controller.module.property;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.estate.EstateRoom;
import com.ruoyi.system.domain.module.estate.EstateRoomAccountRecord;
import com.ruoyi.system.domain.module.property.*;
import com.ruoyi.system.domain.module.excel.PropertySdJyTemplateExcel;
import com.ruoyi.system.domain.module.excel.PropertyWyfTemplateExcel;
import com.ruoyi.system.domain.module.excel.PropertyWyfYearTemplateExcel;
import com.ruoyi.system.service.module.estate.IEstateRoomAccountRecordService;
import com.ruoyi.system.service.module.estate.IEstateRoomService;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.property.*;
import com.ruoyi.web.controller.common.BaseController;
import lombok.RequiredArgsConstructor;

import javax.validation.constraints.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物业费相关费用的模板Controller
 *
 * @author ruoyi
 * @date 2022-06-27
 */
@Validated
@Api(value = "物业费相关费用的模板控制器", tags = {"物业费相关费用的模板管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/property/feeOrder/template")
public class PropertyFeeOrderTemplateController extends BaseController {

	private final IPropertyFeeOrderTemplateService propertyFeeOrderTemplateService;
	private final IEstateRoomService estateRoomService;
	private final IPropertyFeeOrderService propertyFeeOrderService;
	private final IEstateService estateService;
	private final IPropertyService propertyService;
	private final IPropertyFeeOrderOfflineInfoService propertyFeeOrderOfflineInfoService;
	private final IPropertyFeeOrderInfoService propertyFeeOrderInfoService;
	private final IEstateRoomAccountRecordService estateRoomAccountRecordService;
	private final IPropertyFeeOrderTemplateApproverInfoService propertyFeeOrderTemplateApproverInfoService;

	/**
	 * 查询物业费相关费用的模板列表
	 */
	@ApiOperation("查询物业费相关费用的模板列表")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:list')")
	@GetMapping("/list")
	public TableDataInfo<PropertyFeeOrderTemplate> list(@Validated PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		QueryWrapper<PropertyFeeOrderTemplate> qw = new QueryWrapper<>();
		if (propertyFeeOrderTemplate.getEstateId() != null) {
			qw.eq("estate_id", propertyFeeOrderTemplate.getEstateId());
		}
		if (propertyFeeOrderTemplate.getRoomId() != null) {
			qw.eq("room_id", propertyFeeOrderTemplate.getRoomId());
		}
		if (propertyFeeOrderTemplate.getRoomNo() != null) {
			qw.eq("room_no", propertyFeeOrderTemplate.getRoomNo());
		}
		if (propertyFeeOrderTemplate.getFeeType() != null) {
			qw.eq("fee_type", propertyFeeOrderTemplate.getFeeType());
		}
		if (StrUtil.isNotEmpty(propertyFeeOrderTemplate.getBeginDate())) {
			qw.between("pay_time", propertyFeeOrderTemplate.getBeginDate() + " 00:00:00", propertyFeeOrderTemplate.getEndDate() + " 23:59:59");
		}
		if (propertyFeeOrderTemplate.getState() != null) {
			qw.eq("state", propertyFeeOrderTemplate.getState());
		}
		if (propertyFeeOrderTemplate.getRealName() != null) {
			qw.like("real_name", propertyFeeOrderTemplate.getRealName());
		}
		if (propertyFeeOrderTemplate.getYearNum() != null && !propertyFeeOrderTemplate.getYearNum().equals("0")) {
			qw.eq("year_num", propertyFeeOrderTemplate.getYearNum());
		}
		if (propertyFeeOrderTemplate.getMonthNum() != null) {
			qw.eq("month_num", propertyFeeOrderTemplate.getMonthNum());
		}
		if (propertyFeeOrderTemplate.getIsArrears() != null) {
			qw.eq("is_arrears", propertyFeeOrderTemplate.getIsArrears());
		}
		if (propertyFeeOrderTemplate.getRealName() != null) {
			qw.and(wrapper -> {
				wrapper.like("real_name", propertyFeeOrderTemplate.getRealName())
					.or().like("phone_num", propertyFeeOrderTemplate.getRealName())
					.or().like("room_no", propertyFeeOrderTemplate.getRealName());
			});
		}

//		if(StrUtil.isNotEmpty(propertyFeeOrderTemplate.getBeginDate())){
//			qw.between("create_time",propertyFeeOrderTemplate.getBeginDate() + " 00:00:00",propertyFeeOrderTemplate.getEndDate() + " 23:59:59");
//		}
		qw.orderByAsc("room_no,year_num,month_num");

		TableDataInfo<PropertyFeeOrderTemplate> td = PageUtils.buildDataInfo(propertyFeeOrderTemplateService.page(PageUtils.buildPage(), qw));
		List<PropertyFeeOrderTemplate> list = td.getRows();
//		if (propertyFeeOrderTemplate.getState()!=null){
//			if (propertyFeeOrderTemplate.getState()==1 && propertyFeeOrderTemplate.getXianshangBeginDate()!=null){
//				List<PropertyFeeOrderTemplate> list2 = new ArrayList<>();
//				QueryWrapper<PropertyFeeOrder> qw1 = new QueryWrapper<>();
//				if(propertyFeeOrderTemplate.getXianshangBeginDate()!=null){
//					qw1.between("pay_time",propertyFeeOrderTemplate.getXianshangBeginDate(),propertyFeeOrderTemplate.getXianshangEndDate());
//				}
//				List<PropertyFeeOrder> list1 = propertyFeeOrderService.list(qw1);
//				ArrayList<Integer> list4 = new ArrayList<Integer>();
//				for (PropertyFeeOrder list3:list1){
//					list4.add(Math.toIntExact(list3.getId()));
//				}
//				if (list4.size()!=0){
//					for (int i=0;i<=list4.size()-1;i++){
//						for (PropertyFeeOrderTemplate list5:list){
//							System.out.println(list4.get(i));
//							System.out.println(list5.getFeeOrderId());
//							if (list4.get(i) .equals(list5.getFeeOrderId())){
//								list2.add(list5);
//							}
//						}
//					}
//				}
//				td.setRows(list2);
//			}
//		}
		return td;
	}

	@ApiOperation("查询物业费相关费用的模板列表")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:list')")
	@GetMapping("/approverTemplateList")
	public List<PropertyFeeOrderTemplate> approverTemplateList(@Validated PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		if (propertyFeeOrderTemplate.getApproverId() != null) {
			QueryWrapper<PropertyFeeOrderTemplateApproverInfo> qwInfo = new QueryWrapper<>();
			qwInfo.eq("approver_id", propertyFeeOrderTemplate.getApproverId());
			List<PropertyFeeOrderTemplateApproverInfo> infoList = propertyFeeOrderTemplateApproverInfoService.list(qwInfo);
			List<PropertyFeeOrderTemplate> list = new ArrayList<>();
			if (infoList.size() > 0) {
				for (PropertyFeeOrderTemplateApproverInfo info : infoList) {
					PropertyFeeOrderTemplate template = propertyFeeOrderTemplateService.findById(info.getTemplateId());
					if (template != null) {
						list.add(template);
					}
				}
			}
			return list;
		} else {
			return null;
		}
	}


	/**
	 * 查询物业费相关费用的模板列表
	 */
	@ApiOperation("查询物业费相关费用的模板列表")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:list')")
	@GetMapping("/nub")
	public List nub(@Validated PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		QueryWrapper<PropertyFeeOrderTemplate> qw = new QueryWrapper<>();
		if (propertyFeeOrderTemplate.getEstateId() != null) {
			qw.eq("estate_id", propertyFeeOrderTemplate.getEstateId());
		}
//		TableDataInfo<PropertyFeeOrderTemplate> td = PageUtils.buildDataInfo(propertyFeeOrderTemplateService.page(PageUtils.buildPage(),qw));
		List<PropertyFeeOrderTemplate> list = propertyFeeOrderTemplateService.list(qw);
		double yingjiao = 0.00;
		double weijiao = 0.00;
		double yijiao = 0.00;
		for (PropertyFeeOrderTemplate list1 : list) {
			Double feiyong1 = list1.getTotalPrice().doubleValue();
			yingjiao += feiyong1;
			if (list1.getState() == 0) {
				weijiao += feiyong1;
			} else {
				yijiao += feiyong1;
			}
		}
		double shoufeilv = yijiao / yingjiao;
		DecimalFormat df = new DecimalFormat("#0.00");
		DecimalFormat bili = new DecimalFormat("#0");
		List list1 = new ArrayList<>();
		list1.add(df.format(yingjiao) + "元");
		list1.add(df.format(weijiao) + "元");
		list1.add(df.format(yijiao) + "元");
		list1.add(bili.format(shoufeilv) + "%");
		return list1;
	}

	/**
	 * 查询物业费相关费用的模板列表
	 */
	@ApiOperation("查询物业费相关费用的模板列表")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:list')")
	@GetMapping("/nubInfo")
	public List nubInfo(@Validated PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		QueryWrapper<PropertyFeeOrderTemplate> qw = new QueryWrapper<>();
		if (propertyFeeOrderTemplate.getEstateId() != null) {
			qw.eq("estate_id", propertyFeeOrderTemplate.getEstateId());
		}
		if (propertyFeeOrderTemplate.getRoomId() != null) {
			qw.eq("room_id", propertyFeeOrderTemplate.getRoomId());
		}
		if (propertyFeeOrderTemplate.getState() != null) {
			qw.eq("state", propertyFeeOrderTemplate.getState());
		}
		if (StrUtil.isNotEmpty(propertyFeeOrderTemplate.getBeginDate())) {
			qw.between("pay_time", propertyFeeOrderTemplate.getBeginDate() + " 00:00:00", propertyFeeOrderTemplate.getEndDate() + " 23:59:59");
		}
		List<PropertyFeeOrderTemplate> list = propertyFeeOrderTemplateService.list(qw);
		double yingjiao = 0.00;
		double weijiao = 0.00;
		double yingjiaoWyf = 0.00;
		double weijiaoWyf = 0.00;
		double yingjiaoDian = 0.00;
		double weijiaoDian = 0.00;
		double yingjiaoWater = 0.00;
		double weijiaoWater = 0.00;
		for (PropertyFeeOrderTemplate list1 : list) {
			Double feiyong1 = list1.getTotalPrice().doubleValue();
			yingjiao += feiyong1;
			if (list1.getState() == 0) {
				weijiao += feiyong1;
			}
			if (list1.getFeeType().equals("wyf")) {
				yingjiaoWyf += feiyong1;
				if (list1.getState() == 0) {
					weijiaoWyf += feiyong1;
				}
			} else if (list1.getFeeType().equals("eletrict")) {
				yingjiaoDian += feiyong1;
				if (list1.getState() == 0) {
					weijiaoDian += feiyong1;
				}
			} else if (list1.getFeeType().equals("water")) {
				yingjiaoWater += feiyong1;
				if (list1.getState() == 0) {
					weijiaoWater += feiyong1;
				}
			}
		}
		DecimalFormat df = new DecimalFormat("#0.00");
		List list1 = new ArrayList<>();
		list1.add(df.format(yingjiao) + "元");
		list1.add(df.format(weijiao) + "元");
		list1.add(df.format(yingjiaoWyf) + "元");
		list1.add(df.format(weijiaoWyf) + "元");
		list1.add(df.format(yingjiaoDian) + "元");
		list1.add(df.format(weijiaoDian) + "元");
		list1.add(df.format(yingjiaoWater) + "元");
		list1.add(df.format(weijiaoWater) + "元");
		return list1;
	}

	/**
	 * 导出物业费相关费用的模板列表
	 */
	@ApiOperation("导出物业费相关费用的模板列表")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:export')")
	@Log(title = "物业费相关费用的模板", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<PropertyFeeOrderTemplate> export(@Validated PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		QueryWrapper<PropertyFeeOrderTemplate> qw = new QueryWrapper<>();
		if (propertyFeeOrderTemplate.getEstateId() != null) {
			qw.eq("estate_id", propertyFeeOrderTemplate.getEstateId());
		}
		if (propertyFeeOrderTemplate.getRoomId() != null) {
			qw.eq("room_id", propertyFeeOrderTemplate.getRoomId());
		}
		if (propertyFeeOrderTemplate.getRoomNo() != null) {
			qw.eq("room_no", propertyFeeOrderTemplate.getRoomNo());
		}
		if (propertyFeeOrderTemplate.getFeeType() != null && ObjectUtil.isNotEmpty(propertyFeeOrderTemplate.getType())) {
			qw.eq("fee_type", propertyFeeOrderTemplate.getFeeType());
		}
		if (propertyFeeOrderTemplate.getState() != null) {
			qw.eq("state", propertyFeeOrderTemplate.getState());
		}
		if (StrUtil.isNotEmpty(propertyFeeOrderTemplate.getBeginDate())) {
			qw.between("pay_time", propertyFeeOrderTemplate.getBeginDate() + " 00:00:00", propertyFeeOrderTemplate.getEndDate() + " 23:59:59");
		}
		if (propertyFeeOrderTemplate.getRealName() != null) {
			qw.like("real_name", propertyFeeOrderTemplate.getRealName());
		}

		if (propertyFeeOrderTemplate.getYearNum() != null) {
			qw.eq("year_num", propertyFeeOrderTemplate.getYearNum());
		}

		if (propertyFeeOrderTemplate.getMonthNum() != null) {
			qw.eq("month_num", propertyFeeOrderTemplate.getMonthNum());
		}

		if (propertyFeeOrderTemplate.getIsArrears() != null) {
			qw.eq("is_arrears", propertyFeeOrderTemplate.getIsArrears());
		}

		if (propertyFeeOrderTemplate.getRealName() != null) {
			qw.and(wrapper -> {
				wrapper.like("real_name", propertyFeeOrderTemplate.getRealName())
					.or().like("phone_num", propertyFeeOrderTemplate.getRealName())
					.or().like("room_no", propertyFeeOrderTemplate.getRealName());
			});
		}

		List<PropertyFeeOrderTemplate> list = propertyFeeOrderTemplateService.list(qw);
//		if (propertyFeeOrderTemplate.getState()==1 && propertyFeeOrderTemplate.getXianshangBeginDate()!=null){
//			List<PropertyFeeOrderTemplate> list2 = new ArrayList<>();
//			QueryWrapper<PropertyFeeOrder> qw1 = new QueryWrapper<>();
//			if(propertyFeeOrderTemplate.getXianshangBeginDate()!=null){
//				qw1.between("pay_time",propertyFeeOrderTemplate.getXianshangBeginDate(),propertyFeeOrderTemplate.getXianshangEndDate());
//			}
//			List<PropertyFeeOrder> list1 = propertyFeeOrderService.list(qw1);
//			ArrayList<Integer> list4 = new ArrayList<Integer>();
//			for (PropertyFeeOrder list3:list1){
//				list4.add(Math.toIntExact(list3.getId()));
//			}
//			if (list4.size()!=0){
//				for (int i=0;i<=list4.size()-1;i++){
//					for (PropertyFeeOrderTemplate list5:list){
//						System.out.println(list4.get(i));
//						System.out.println(list5.getFeeOrderId());
//						if (list4.get(i) .equals(list5.getFeeOrderId())){
//							list2.add(list5);
//						}
//					}
//				}
//			}
//			list=list2;
//		}
		if (CollUtil.isNotEmpty(list)) {
			for (PropertyFeeOrderTemplate template : list) {
//				template.setCreateTimeStr(DateUtils.getTime(template.getCreateTime()));
				if (template.getState() == 0) {
					template.setStateStr("未交费");
				} else if (template.getState() == 1) {
					template.setStateStr("已交费");
				} else if (template.getState() == 2) {
					template.setStateStr("线下交费");
				}
				if (template.getFeeType().equals("wyf")) {
					template.setFeeTypeName("物业费");
				} else if (template.getFeeType().equals("rubbish")) {
					template.setFeeTypeName("垃圾处理费");
				} else if (template.getFeeType().equals("car")) {
					template.setFeeTypeName("车位费");
				} else if (template.getFeeType().equals("water")) {
					template.setFeeTypeName("代收水费");
				} else if (template.getFeeType().equals("eletrict")) {
					template.setFeeTypeName("代收电费");
				} else if (template.getFeeType().equals("jiaya")) {
					template.setFeeTypeName("加压费");
				}
				if (template.getPayTime() != null) {
					template.setPayTimeEx(DateUtils.getTime(template.getPayTime()));
				}
			}
		}
		ExcelUtil<PropertyFeeOrderTemplate> util = new ExcelUtil<>(PropertyFeeOrderTemplate.class);
		return util.exportExcel(list, "物业费相关费用的模板");
	}

	/**
	 * 导出物业费相关费用的模板列表
	 */
	@ApiOperation("导出物业费相关费用的模板列表")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:export')")
	@Log(title = "物业费相关费用的模板", businessType = BusinessType.EXPORT)
	@GetMapping("/offlineExport")
	public AjaxResult<PropertyFeeOrderTemplate> offlineExport(@Validated PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		QueryWrapper<PropertyFeeOrderTemplate> qw = new QueryWrapper<>();
		if (propertyFeeOrderTemplate.getEstateId() != null) {
			qw.eq("estate_id", propertyFeeOrderTemplate.getEstateId());
		}
		if (propertyFeeOrderTemplate.getRoomId() != null) {
			qw.eq("room_id", propertyFeeOrderTemplate.getRoomId());
		}
		if (propertyFeeOrderTemplate.getRoomNo() != null) {
			qw.eq("room_no", propertyFeeOrderTemplate.getRoomNo());
		}
		if (propertyFeeOrderTemplate.getFeeType() != null) {
			qw.eq("fee_type", propertyFeeOrderTemplate.getFeeType());
		}
		if (propertyFeeOrderTemplate.getState() != null) {
			qw.eq("state", propertyFeeOrderTemplate.getState());
		}
		if (propertyFeeOrderTemplate.getRealName() != null) {
			qw.like("real_name", propertyFeeOrderTemplate.getRealName());
		}

		if (propertyFeeOrderTemplate.getYearNum() != null) {
			qw.eq("year_num", propertyFeeOrderTemplate.getYearNum());
		}

		if (propertyFeeOrderTemplate.getMonthNum() != null) {
			qw.eq("month_num", propertyFeeOrderTemplate.getMonthNum());
		}

		if (propertyFeeOrderTemplate.getIsArrears() != null) {
			qw.eq("is_arrears", propertyFeeOrderTemplate.getIsArrears());
		}

		if (propertyFeeOrderTemplate.getRealName() != null) {
			qw.and(wrapper -> {
				wrapper.like("real_name", propertyFeeOrderTemplate.getRealName())
					.or().like("phone_num", propertyFeeOrderTemplate.getRealName())
					.or().like("room_no", propertyFeeOrderTemplate.getRealName());
			});
		}

		List<PropertyFeeOrderTemplate> list = propertyFeeOrderTemplateService.list(qw);
		if (CollUtil.isNotEmpty(list)) {
			for (PropertyFeeOrderTemplate template : list) {
//				template.setCreateTimeStr(DateUtils.getTime(template.getCreateTime()));
				if (template.getState() == 0) {
					template.setStateStr("未交费");
				} else if (template.getState() == 1) {
					template.setStateStr("已交费");
				} else if (template.getState() == 2) {
					template.setStateStr("线下交费");
				}
			}
		}
		ExcelUtil<PropertyFeeOrderTemplate> util = new ExcelUtil<>(PropertyFeeOrderTemplate.class);
		return util.exportExcel(list, "物业费相关费用的模板");
	}

	@Log(title = "物业费导入", businessType = BusinessType.IMPORT)
	@PreAuthorize("@ss.hasAnyPermi('module:feeOrderTemp:import')")
	@PostMapping("/importDataWyf")
	public AjaxResult importDataWyf(MultipartFile file, boolean updateSupport, Integer estateId) throws Exception {
		ExcelUtil<PropertyWyfTemplateExcel> util = new ExcelUtil<>(PropertyWyfTemplateExcel.class);
		List<PropertyWyfTemplateExcel> templateExcels = util.importExcel(file.getInputStream());
		SysUser sysUser = getUser();
		String operName = sysUser.getUserName();
		String message = propertyFeeOrderTemplateService.importWyfData(templateExcels, estateId, updateSupport, operName);
		return AjaxResult.success(message);
	}


	@Log(title = "物业费导入", businessType = BusinessType.IMPORT)
	@PreAuthorize("@ss.hasAnyPermi('module:feeOrderTemp:import')")
	@PostMapping("/importDataWyfYear")
	public AjaxResult importDataWyfYear(MultipartFile file, boolean updateSupport, Integer estateId) throws Exception {
		ExcelUtil<PropertyWyfYearTemplateExcel> util = new ExcelUtil<>(PropertyWyfYearTemplateExcel.class);
		List<PropertyWyfYearTemplateExcel> templateExcels = util.importExcel(file.getInputStream());
		SysUser sysUser = getUser();
		String operName = sysUser.getUserName();
		String message = propertyFeeOrderTemplateService.importWyfYearData(templateExcels, estateId, updateSupport, operName);
		return AjaxResult.success(message);
	}

	@Log(title = "水电加压费导入", businessType = BusinessType.IMPORT)
	@PreAuthorize("@ss.hasAnyPermi('module:feeOrderTemp:import')")
	@PostMapping("/importDataSdJ")
	public AjaxResult importDataSdJ(MultipartFile file, boolean updateSupport, Integer estateId) throws Exception {
		ExcelUtil<PropertySdJyTemplateExcel> util = new ExcelUtil<>(PropertySdJyTemplateExcel.class);
		List<PropertySdJyTemplateExcel> templateExcels = util.importExcel(file.getInputStream());
		SysUser sysUser = getUser();
		String operName = sysUser.getUserName();
		String message = propertyFeeOrderTemplateService.importSdJyData(templateExcels, estateId, updateSupport, operName);
		return AjaxResult.success(message);
	}


	/**
	 * 下载物业费导入模板
	 *
	 * @return
	 */
	@GetMapping("/importTemplateWyf")
	public AjaxResult importTemplateWyf() {
		ExcelUtil<PropertyWyfTemplateExcel> util = new ExcelUtil<>(PropertyWyfTemplateExcel.class);
		return util.importTemplateExcel("物业费模板");
	}

	/**
	 * 下载物业费导入模板
	 *
	 * @return
	 */
	@GetMapping("/importTemplateWyfYear")
	public AjaxResult importTemplateWyfYear() {
		ExcelUtil<PropertyWyfTemplateExcel> util = new ExcelUtil<>(PropertyWyfTemplateExcel.class);
		return util.importTemplateExcel("物业费模板");
	}

	/**
	 * 下载数点加压导入模板
	 *
	 * @return
	 */
	@GetMapping("/importTemplateSdJ")
	public AjaxResult importTemplateSdJ() {
		ExcelUtil<PropertySdJyTemplateExcel> util = new ExcelUtil<>(PropertySdJyTemplateExcel.class);
		return util.importTemplateExcel("水电加压模板");
	}


	/**
	 * 获取物业费相关费用的模板详细信息
	 */
	@ApiOperation("获取物业费相关费用的模板详细信息")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:query')")
	@GetMapping("/{id}")
	public AjaxResult<PropertyFeeOrderTemplate> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
		PropertyFeeOrderTemplate propertyFeeOrderTemplate = propertyFeeOrderTemplateService.getById(id);
		if (propertyFeeOrderTemplate != null) {
			propertyFeeOrderTemplate.setIsCanEdit(0);
			if (propertyFeeOrderTemplate.getState() == 0) {
				propertyFeeOrderTemplate.setIsCanEdit(1);
			}
		}
		return AjaxResult.success(propertyFeeOrderTemplate);
	}

	/**
	 * 新增物业费相关费用的模板
	 */
	@ApiOperation("新增物业费相关费用的模板")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:add')")
	@Log(title = "物业费相关费用的模板", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		if (propertyFeeOrderTemplate.getEstateId() != null) {
			propertyFeeOrderTemplate.setEstateName(estateService.findById(propertyFeeOrderTemplate.getEstateId()).getEstateName());
		}
		propertyFeeOrderTemplate.setState(0);
		return toAjax(propertyFeeOrderTemplateService.save(propertyFeeOrderTemplate) ? 1 : 0);
	}

	/**
	 * 修改物业费相关费用的模板
	 */
	@ApiOperation("修改物业费相关费用的模板")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:edit')")
	@Log(title = "物业费相关费用的模板", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping()
	public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		if (propertyFeeOrderTemplate.getState() == 2) {
			Date date = new Date();
			propertyFeeOrderTemplate.setPayTime(date);
		}
		return toAjax(propertyFeeOrderTemplateService.updateById(propertyFeeOrderTemplate) ? 1 : 0);
	}

	/**
	 * 删除物业费相关费用的模板
	 */
	@ApiOperation("删除物业费相关费用的模板")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:remove')")
	@Log(title = "物业费相关费用的模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
		return toAjax(propertyFeeOrderTemplateService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
	}

	/**
	 * 批量修改状态
	 */
	@ApiOperation("批量修改状态")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:edit')")
	@Log(title = "批量修改状态")
	@PostMapping("/batchUpdateOld")
	public AjaxResult<Void> batchUpdateOld(@Validated(AddGroup.class) @RequestBody PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		int count = 0;
		List<Integer> ids = propertyFeeOrderTemplate.getIds();
		if (CollUtil.isNotEmpty(ids)) {
			for (Integer id : ids) {
				PropertyFeeOrderTemplate template = propertyFeeOrderTemplateService.findById(id);
				if (template != null) {
					if (propertyFeeOrderTemplate.getState() != null) {
						template.setState(propertyFeeOrderTemplate.getState());
						if (propertyFeeOrderTemplate.getState() == 2) {
							Date date = new Date();
							template.setPayTime(date);
						}
					}
					if (propertyFeeOrderTemplate.getRemark() != null) {
						template.setRemark(propertyFeeOrderTemplate.getRemark());
					}
					boolean bl = propertyFeeOrderTemplateService.updateById(template);
					if (bl) {
						count++;
					}
				}
			}
		}
		return AjaxResult.success("成功修改" + count + "条数据");
	}

	/**
	 * 线下缴费
	 */
	@ApiOperation("批量修改状态")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:edit')")
	@Log(title = "批量修改状态")
	@PostMapping("/batchUpdate")
	public AjaxResult<Void> batchUpdate(@Validated(AddGroup.class) @RequestBody PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		SysUser sysUser = getUser();
		//新增物业缴费记录
		PropertyFeeOrder feeOrder = new PropertyFeeOrder();
		if (propertyFeeOrderTemplate.getRoomId() != null) {
			EstateRoom room = estateRoomService.findById(propertyFeeOrderTemplate.getRoomId());
			if (room != null) {
				feeOrder.setEstateId(room.getEstateId());
				feeOrder.setRealName(room.getRealName());
				feeOrder.setRoomNo(room.getRoomNo());
				feeOrder.setPhoneNum(room.getPhoneNum());
				feeOrder.setRemark(propertyFeeOrderTemplate.getRemake());
				if (room.getRoomArea() != null) {
					feeOrder.setRoomAreas(Double.valueOf(room.getRoomArea()));
				}
				feeOrder.setRoomType(room.getRoomType());
				if (propertyFeeOrderTemplate.getRechargeWay() == 1) {
					feeOrder.setPayPlatform(2);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 2) {
					feeOrder.setPayPlatform(3);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 3) {
					feeOrder.setPayPlatform(4);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 4) {
					feeOrder.setPayPlatform(5);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 5) {
					feeOrder.setPayPlatform(6);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 6) {
					feeOrder.setPayPlatform(7);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 7) {
					feeOrder.setPayPlatform(8);
				}
				Estate estate = estateService.findById(room.getEstateId());
				if (estate != null) {
					feeOrder.setPropertyId(estate.getPropertyId());
					feeOrder.setEstateName(estate.getEstateName());
					Property property = propertyService.findById(estate.getPropertyId());
					if (property != null) {
						feeOrder.setPropertyName(property.getPropertyName());
					}
				}
			}
			feeOrder.setRoomId(propertyFeeOrderTemplate.getRoomId());
		}
		feeOrder.setPlatform("web");
		feeOrder.setUserId(0);
		feeOrder.setNickName(sysUser.getNickName());
		feeOrder.setPayMoney(BigDecimal.valueOf(propertyFeeOrderTemplate.getViewSum()));
		if (propertyFeeOrderTemplate.getDiscount() != null) {
			if (propertyFeeOrderTemplate.getDiscount() != 10) {
				feeOrder.setWyfYhRate(BigDecimal.valueOf(propertyFeeOrderTemplate.getDiscount()));
			} else {
				feeOrder.setWyfYhRate(BigDecimal.valueOf(0.00));
			}
		} else {
			feeOrder.setWyfYhRate(BigDecimal.valueOf(0.00));
		}

		Date dates = new Date();
		Long dateInt = dates.getTime();
		String orderNo = "37UT1" + dateInt;
		feeOrder.setOrderNo(orderNo);
		feeOrder.setJiaYaYouhui(BigDecimal.valueOf(0.00));
		feeOrder.setCarYouhui(BigDecimal.valueOf(0.00));
		String beginDate = "2024-1-1";
		Date begin = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			begin = format.parse(beginDate);
		} catch (Exception e) {
			System.out.println("日期格式错误！");
		}
		String endDate = "2025-12-31";
		Date end = new Date();
		SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd");
		try {
			end = formats.parse(endDate);
		} catch (Exception e) {
			System.out.println("日期格式错误！");
		}
		feeOrder.setEndTime(end);
		feeOrder.setState(4);
		feeOrder.setDkRate(propertyFeeOrderTemplate.getDiscount());
		Date date1 = new Date();
		feeOrder.setPayTime(date1);
		feeOrder.setIsDel(0);
		QueryWrapper<PropertyFeeOrderTemplate> qw = new QueryWrapper<>();
		qw.eq("room_id", propertyFeeOrderTemplate.getRoomId());
		List<PropertyFeeOrderTemplate> templates = propertyFeeOrderTemplateService.list(qw);
		for (PropertyFeeOrderTemplate pt : templates) {
			if (pt.getFeeType().equals("wyf")) {
				feeOrder.setWyfSinglePrice(pt.getSinglePrice());
			}
			if (pt.getFeeType().equals("water")) {
				feeOrder.setWaterSinglePrice(pt.getSinglePrice());
			}
			if (pt.getFeeType().equals("eletrict")) {
				feeOrder.setElectricSinglePrice(pt.getSinglePrice());
			}
			if (pt.getFeeType().equals("car")) {
				feeOrder.setCarSinglePrice(pt.getSinglePrice());
			}
			if (pt.getFeeType().equals("rubbish")) {
				feeOrder.setTrashFee(pt.getSinglePrice());
			}
		}
		int count = 0;
		double wyf = 0.00;
		double wyfYh = 0.00;
		int wyfMonth = 0;
		double water = 0.00;
		int waterMonth = 0;
		double electric = 0.00;
		int electricMonth = 0;
		double car = 0.00;
		int carMonth = 0;
		double rubbish = 0.00;
		double rubbishYh = 0.00;
		int rubbishMonth = 0;
		double jiaya = 0.00;
		int jiayaMonth = 0;
		List<Integer> ids = propertyFeeOrderTemplate.getIds();
		//对id进行排序
		Collections.sort(ids);
		if (CollUtil.isNotEmpty(ids)) {
			//循环缴费id清单
			for (Integer id : ids) {
				PropertyFeeOrderTemplate template = propertyFeeOrderTemplateService.findById(id);
				if (template != null) {
					if (template.getFeeType().equals("wyf")) {
						wyf += template.getTotalPrice().doubleValue();
						wyfYh += template.getYouhuiPrice().doubleValue();
						wyfMonth += 1;
					}
					if (template.getFeeType().equals("water")) {
						water += template.getTotalPrice().doubleValue();
						wyfMonth += 1;
					}
					if (template.getFeeType().equals("eletrict")) {
						electric += template.getTotalPrice().doubleValue();
						electricMonth += 1;
					}
					if (template.getFeeType().equals("car")) {
						car += template.getTotalPrice().doubleValue();
						carMonth += 1;
					}
					if (template.getFeeType().equals("rubbish")) {
						rubbish += template.getTotalPrice().doubleValue();
						rubbishYh += template.getYouhuiPrice().doubleValue();
						rubbishMonth += 1;
					}
					if (template.getFeeType().equals("jiaya")) {
						jiaya += template.getTotalPrice().doubleValue();
						jiayaMonth += 1;
					}
					if (propertyFeeOrderTemplate.getState() != null) {
						template.setState(propertyFeeOrderTemplate.getState());
						if (propertyFeeOrderTemplate.getState() == 2) {
							Date date = new Date();
							template.setPayTime(date);
						}
					}
					if (propertyFeeOrderTemplate.getRemark() != null) {
						template.setRemark(propertyFeeOrderTemplate.getRemark());
					}
					boolean bl = propertyFeeOrderTemplateService.updateById(template);
					if (bl) {
						count++;
					}
				}
			}
			if (propertyFeeOrderTemplate.getDiscount() != null) {
				feeOrder.setWyfFee(BigDecimal.valueOf(wyf * propertyFeeOrderTemplate.getDiscount() / 10));
				feeOrder.setWaterFee(BigDecimal.valueOf(water));
				feeOrder.setElectricFee(BigDecimal.valueOf(electric));
				feeOrder.setTrashFee(BigDecimal.valueOf(rubbish));
				feeOrder.setJiaYaFee(BigDecimal.valueOf(jiaya));
				feeOrder.setCarFee(BigDecimal.valueOf(car));
				feeOrder.setWyfYhPrice(BigDecimal.valueOf(wyfYh));
				feeOrder.setTrashYouhui(BigDecimal.valueOf(rubbishYh));
			}
			feeOrder.setWyfMonths(String.valueOf(wyfMonth));
			feeOrder.setWaterMonths(String.valueOf(waterMonth));
			feeOrder.setElectricMonths(String.valueOf(electricMonth));
			feeOrder.setCarMonths(String.valueOf(carMonth));
			feeOrder.setTotalMoney(BigDecimal.valueOf((wyf * propertyFeeOrderTemplate.getDiscount() / 10) + water + electric + rubbish + jiaya + car));
			if (feeOrder != null) {
				propertyFeeOrderService.save(feeOrder);
				//添加线下缴费详情
				for (Integer id : propertyFeeOrderTemplate.getIds()) {
					PropertyFeeOrderOfflineInfo info = new PropertyFeeOrderOfflineInfo();
					info.setOrderId(feeOrder.getId());
					info.setTemplateId(Long.valueOf(id));
					propertyFeeOrderOfflineInfoService.save(info);
				}
				//添加物业费费用详情
				if (!feeOrder.getWyfFee().equals(BigDecimal.valueOf(0.0))) {
					PropertyFeeOrderInfo info = new PropertyFeeOrderInfo();
					info.setOrderId(feeOrder.getId());
					info.setUserId(0);
					info.setEstateId(feeOrder.getEstateId());
					info.setRoomId(feeOrder.getRoomId());
					info.setTotalPrice(feeOrder.getTotalMoney());
					info.setPayPrice(feeOrder.getPayMoney());
					info.setState(1);
					//添加日期
					String yearBeging = "";
					String yearEnd = "";
					String monthBeging = "";
					String monthEnd = "";
					String dateBeging = "01";
					String dateEnd = "28";
					List<PropertyFeeOrderTemplate> templateList = new ArrayList<>();
					List<PropertyFeeOrderTemplate> fujiaList = new ArrayList<>();
					for (Integer id : propertyFeeOrderTemplate.getIds()) {
						PropertyFeeOrderTemplate template = propertyFeeOrderTemplateService.findById(id);
						if (template.getFeeType().equals("wyf")) {
							templateList.add(template);
							info.setFeeType("wyf");
						}
						if (template.getFeeType().equals("rubbish")) {
							fujiaList.add(template);
						}
					}
					Double zhuFee = 0.00;
					Double fuFee = 0.00;
					Double zhuYouhui = 0.00;
					Double fuYouhui = 0.00;
					if (templateList.size() > 0) {
						for (int i = 0; i < templateList.size(); i++) {
							yearBeging = templateList.get(0).getYearNum();
							yearEnd = templateList.get(templateList.size() - 1).getYearNum();
							monthBeging = templateList.get(0).getMonthNum();
							monthEnd = templateList.get(templateList.size() - 1).getMonthNum();
							info.setSinglePrice(templateList.get(i).getSinglePrice());
							info.setMonthCount(templateList.size());
							zhuYouhui += templateList.get(i).getYouhuiPrice().doubleValue();
							zhuFee += templateList.get(i).getTotalPrice().doubleValue();
						}

					}
					if (fujiaList.size() > 0) {
						for (int i = 0; i < fujiaList.size(); i++) {
							yearBeging = fujiaList.get(0).getYearNum();
							yearEnd = fujiaList.get(fujiaList.size() - 1).getYearNum();
							monthBeging = fujiaList.get(0).getMonthNum();
							monthEnd = fujiaList.get(fujiaList.size() - 1).getMonthNum();
							info.setSinglePrice(fujiaList.get(i).getSinglePrice());
							info.setMonthCount(fujiaList.size());
							fuYouhui += fujiaList.get(i).getYouhuiPrice().doubleValue();
							fuFee += fujiaList.get(i).getTotalPrice().doubleValue();
						}
					}
					info.setTotalPrice(BigDecimal.valueOf(zhuFee));
					info.setAttachPrice(BigDecimal.valueOf(fuFee));
					info.setYouhuiPrice(BigDecimal.valueOf(zhuYouhui));
					info.setAttachYouhuiPrice(BigDecimal.valueOf(fuYouhui));
					if (!monthEnd.equals("10") && !monthEnd.equals("11") && !monthEnd.equals("12")) {
						monthEnd = "0" + monthEnd;
					}
					if (!monthBeging.equals("10") && !monthBeging.equals("11") && !monthBeging.equals("12")) {
						monthBeging = "0" + monthBeging;
					}
					String begingTime = yearBeging + "-" + monthBeging + "-" + dateBeging;
					String endTime = yearEnd + "-" + monthEnd + "-" + dateEnd;
					DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
					LocalDate BegingTime = LocalDate.parse(begingTime, formatter);
					LocalDate EndTime = LocalDate.parse(endTime, formatter);
					ZonedDateTime zonedDateTime1 = BegingTime.atStartOfDay(ZoneId.systemDefault());
					Date Date1 = Date.from(zonedDateTime1.toInstant());

					ZonedDateTime zonedDateTime2 = EndTime.atStartOfDay(ZoneId.systemDefault());
					Date Date2 = Date.from(zonedDateTime2.toInstant());

					info.setBeginDate(Date1);
					info.setEndDate(Date2);

					if (fujiaList.size() > 0) {
						info.setAttachSinglePrice(fujiaList.get(0).getSinglePrice());
						info.setAttachPrice(feeOrder.getTrashFee());
					}
					propertyFeeOrderInfoService.save(info);
				}
				//添加水费费用详情
				if (!feeOrder.getWaterFee().equals(BigDecimal.valueOf(0.0))) {
					PropertyFeeOrderInfo info = new PropertyFeeOrderInfo();
					info.setOrderId(feeOrder.getId());
					info.setUserId(0);
					info.setEstateId(feeOrder.getEstateId());
					info.setRoomId(feeOrder.getRoomId());
					info.setTotalPrice(feeOrder.getTotalMoney());
					info.setPayPrice(feeOrder.getPayMoney());
					info.setState(1);
					//添加日期
					String yearBeging = "";
					String yearEnd = "";
					String monthBeging = "";
					String monthEnd = "";
					String dateBeging = "01";
					String dateEnd = "28";
					List<PropertyFeeOrderTemplate> templateList = new ArrayList<>();
					List<PropertyFeeOrderTemplate> fujiaList = new ArrayList<>();
					for (Integer id : propertyFeeOrderTemplate.getIds()) {
						PropertyFeeOrderTemplate template = propertyFeeOrderTemplateService.findById(id);
						if (template.getFeeType().equals("water")) {
							templateList.add(template);
							info.setFeeType("water");
						}
						if (template.getFeeType().equals("jiaya")) {
							fujiaList.add(template);
						}
					}
					Double zhuFee = 0.00;
					Double fuFee = 0.00;
					if (templateList.size() > 0) {
						for (int i = 0; i < templateList.size(); i++) {
							yearBeging = templateList.get(i).getYearNum();
							yearEnd = templateList.get(templateList.size() - 1).getYearNum();
							monthBeging = templateList.get(i).getMonthNum();
							monthEnd = templateList.get(templateList.size() - 1).getMonthNum();
							info.setBeginCount(templateList.get(i).getBeginCount().doubleValue());
							info.setEndCount(templateList.get(i).getEndCount().doubleValue());
							info.setCount(templateList.get(i).getPaidCount().doubleValue());
							info.setSinglePrice(templateList.get(i).getSinglePrice());
							info.setMonthCount(templateList.size());
							zhuFee += templateList.get(i).getTotalPrice().doubleValue();
						}

					}
					if (fujiaList.size() > 0) {
						for (int i = 0; i < fujiaList.size(); i++) {
							yearBeging = fujiaList.get(i).getYearNum();
							yearEnd = fujiaList.get(fujiaList.size() - 1).getYearNum();
							monthBeging = fujiaList.get(i).getMonthNum();
							monthEnd = fujiaList.get(fujiaList.size() - 1).getMonthNum();
							info.setSinglePrice(fujiaList.get(i).getSinglePrice());
							info.setMonthCount(fujiaList.size());
							fuFee += fujiaList.get(i).getTotalPrice().doubleValue();
						}
					}
					info.setTotalPrice(BigDecimal.valueOf(zhuFee));
					info.setAttachPrice(BigDecimal.valueOf(fuFee));
					if (!monthEnd.equals("10") && !monthEnd.equals("11") && !monthEnd.equals("12")) {
						monthEnd = "0" + monthEnd;
					}
					if (!monthBeging.equals("10") && !monthBeging.equals("11") && !monthBeging.equals("12")) {
						monthBeging = "0" + monthBeging;
					}
					String begingTime = yearBeging + "-" + monthBeging + "-" + dateBeging;
					String endTime = yearEnd + "-" + monthEnd + "-" + dateEnd;
					DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
					LocalDate BegingTime = LocalDate.parse(begingTime, formatter);
					LocalDate EndTime = LocalDate.parse(endTime, formatter);
					ZonedDateTime zonedDateTime1 = BegingTime.atStartOfDay(ZoneId.systemDefault());
					Date Date1 = Date.from(zonedDateTime1.toInstant());

					ZonedDateTime zonedDateTime2 = EndTime.atStartOfDay(ZoneId.systemDefault());
					Date Date2 = Date.from(zonedDateTime2.toInstant());

					info.setBeginDate(Date1);
					info.setEndDate(Date2);

					if (fujiaList.size() > 0) {
						info.setAttachSinglePrice(fujiaList.get(0).getSinglePrice());
						info.setAttachPrice(feeOrder.getTrashFee());
					}
					propertyFeeOrderInfoService.save(info);
				}

				//添加电费费用详情
				if (!feeOrder.getElectricFee().equals(BigDecimal.valueOf(0.0))) {
					PropertyFeeOrderInfo info = new PropertyFeeOrderInfo();
					info.setOrderId(feeOrder.getId());
					info.setUserId(0);
					info.setEstateId(feeOrder.getEstateId());
					info.setRoomId(feeOrder.getRoomId());
					info.setTotalPrice(feeOrder.getTotalMoney());
					info.setPayPrice(feeOrder.getPayMoney());
					info.setState(1);
					//添加日期
					String yearBeging = "";
					String yearEnd = "";
					String monthBeging = "";
					String monthEnd = "";
					String dateBeging = "01";
					String dateEnd = "28";
					List<PropertyFeeOrderTemplate> templateList = new ArrayList<>();
					List<PropertyFeeOrderTemplate> fujiaList = new ArrayList<>();
					for (Integer id : propertyFeeOrderTemplate.getIds()) {
						PropertyFeeOrderTemplate template = propertyFeeOrderTemplateService.findById(id);
						if (template.getFeeType().equals("eletrict")) {
							templateList.add(template);
							info.setFeeType("eletrict");
						}
					}
					Double zhuFee = 0.00;
					if (templateList.size() > 0) {
						for (int i = 0; i < templateList.size(); i++) {
							yearBeging = templateList.get(i).getYearNum();
							yearEnd = templateList.get(templateList.size() - 1).getYearNum();
							monthBeging = templateList.get(i).getMonthNum();
							monthEnd = templateList.get(templateList.size() - 1).getMonthNum();
							info.setBeginCount(templateList.get(i).getBeginCount().doubleValue());
							info.setEndCount(templateList.get(i).getEndCount().doubleValue());
							info.setCount(templateList.get(i).getPaidCount().doubleValue());
							info.setSinglePrice(templateList.get(i).getSinglePrice());
							info.setMonthCount(templateList.size());
							zhuFee += templateList.get(i).getTotalPrice().doubleValue();
						}

					}
					info.setTotalPrice(BigDecimal.valueOf(zhuFee));
					if (!monthEnd.equals("10") && !monthEnd.equals("11") && !monthEnd.equals("12")) {
						monthEnd = "0" + monthEnd;
					}
					if (!monthBeging.equals("10") && !monthBeging.equals("11") && !monthBeging.equals("12")) {
						monthBeging = "0" + monthBeging;
					}
					String begingTime = yearBeging + "-" + monthBeging + "-" + dateBeging;
					String endTime = yearEnd + "-" + monthEnd + "-" + dateEnd;
					DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
					LocalDate BegingTime = LocalDate.parse(begingTime, formatter);
					LocalDate EndTime = LocalDate.parse(endTime, formatter);
					ZonedDateTime zonedDateTime1 = BegingTime.atStartOfDay(ZoneId.systemDefault());
					Date Date1 = Date.from(zonedDateTime1.toInstant());

					ZonedDateTime zonedDateTime2 = EndTime.atStartOfDay(ZoneId.systemDefault());
					Date Date2 = Date.from(zonedDateTime2.toInstant());

					info.setBeginDate(Date1);
					info.setEndDate(Date2);

					if (fujiaList.size() > 0) {
						info.setAttachSinglePrice(fujiaList.get(0).getSinglePrice());
						info.setAttachPrice(feeOrder.getTrashFee());
					}
					propertyFeeOrderInfoService.save(info);
				}

				//添加车位费费用详情
				if (!feeOrder.getCarFee().equals(BigDecimal.valueOf(0.0))) {
					PropertyFeeOrderInfo info = new PropertyFeeOrderInfo();
					info.setOrderId(feeOrder.getId());
					info.setUserId(0);
					info.setEstateId(feeOrder.getEstateId());
					info.setRoomId(feeOrder.getRoomId());
					info.setTotalPrice(feeOrder.getTotalMoney());
					info.setPayPrice(feeOrder.getPayMoney());
					info.setState(1);
					//添加日期
					String yearBeging = "";
					String yearEnd = "";
					String monthBeging = "";
					String monthEnd = "";
					String dateBeging = "01";
					String dateEnd = "28";
					List<PropertyFeeOrderTemplate> templateList = new ArrayList<>();
					List<PropertyFeeOrderTemplate> fujiaList = new ArrayList<>();
					for (Integer id : propertyFeeOrderTemplate.getIds()) {
						PropertyFeeOrderTemplate template = propertyFeeOrderTemplateService.findById(id);
						if (template.getFeeType().equals("car")) {
							templateList.add(template);
							info.setFeeType("car");
						}
					}
					Double zhuFee = 0.00;
					if (templateList.size() > 0) {
						for (int i = 0; i < templateList.size(); i++) {
							yearBeging = templateList.get(i).getYearNum();
							yearEnd = templateList.get(templateList.size() - 1).getYearNum();
							monthBeging = templateList.get(i).getMonthNum();
							monthEnd = templateList.get(templateList.size() - 1).getMonthNum();
							info.setSinglePrice(templateList.get(i).getSinglePrice());
							info.setMonthCount(templateList.size());
							zhuFee += templateList.get(i).getTotalPrice().doubleValue();
						}

					}
					info.setTotalPrice(BigDecimal.valueOf(zhuFee));
					if (!monthEnd.equals("10") && !monthEnd.equals("11") && !monthEnd.equals("12")) {
						monthEnd = "0" + monthEnd;
					}
					if (!monthBeging.equals("10") && !monthBeging.equals("11") && !monthBeging.equals("12")) {
						monthBeging = "0" + monthBeging;
					}
					String begingTime = yearBeging + "-" + monthBeging + "-" + dateBeging;
					String endTime = yearEnd + "-" + monthEnd + "-" + dateEnd;
					DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
					LocalDate BegingTime = LocalDate.parse(begingTime, formatter);
					LocalDate EndTime = LocalDate.parse(endTime, formatter);
					ZonedDateTime zonedDateTime1 = BegingTime.atStartOfDay(ZoneId.systemDefault());
					Date Date1 = Date.from(zonedDateTime1.toInstant());

					ZonedDateTime zonedDateTime2 = EndTime.atStartOfDay(ZoneId.systemDefault());
					Date Date2 = Date.from(zonedDateTime2.toInstant());

					info.setBeginDate(Date1);
					info.setEndDate(Date2);

					if (fujiaList.size() > 0) {
						info.setAttachSinglePrice(fujiaList.get(0).getSinglePrice());
						info.setAttachPrice(feeOrder.getTrashFee());
					}
					propertyFeeOrderInfoService.save(info);
				}
			}
		}
		return AjaxResult.success("成功修改" + count + "条数据");
	}


	/**
	 * 使用套内余额线下缴费
	 */
	@ApiOperation("批量修改状态")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:edit')")
	@Log(title = "批量修改状态")
	@PostMapping("/batchUpdateAccount")
	public AjaxResult<Void> batchUpdateAccount(@Validated(AddGroup.class) @RequestBody PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		SysUser sysUser = getUser();
		//查询套内账户数据
		EstateRoom accountRoom = estateRoomService.findById(propertyFeeOrderTemplate.getRoomId());
		Double account = accountRoom.getAccount();

		//新增物业缴费记录
		PropertyFeeOrder feeOrder = new PropertyFeeOrder();
		//处理实缴金额 及 账户余额
		if (account >= propertyFeeOrderTemplate.getViewSum()) {
			feeOrder.setPayMoney(BigDecimal.valueOf(0));
			feeOrder.setAccountFee(propertyFeeOrderTemplate.getViewSum());
			feeOrder.setRemark(propertyFeeOrderTemplate.getRemake());
			account = account - propertyFeeOrderTemplate.getViewSum();
		} else if (account < propertyFeeOrderTemplate.getViewSum()) {
			feeOrder.setPayMoney(BigDecimal.valueOf(propertyFeeOrderTemplate.getViewSum() - account));
			feeOrder.setAccountFee(account);
			account = Double.valueOf(0);
		}

		if (propertyFeeOrderTemplate.getRoomId() != null) {
			EstateRoom room = estateRoomService.findById(propertyFeeOrderTemplate.getRoomId());
			if (room != null) {
				feeOrder.setEstateId(room.getEstateId());
				feeOrder.setRealName(room.getRealName());
				feeOrder.setRoomNo(room.getRoomNo());
				feeOrder.setPhoneNum(room.getPhoneNum());
				feeOrder.setRoomAreas(Double.valueOf(room.getRoomArea()));
				feeOrder.setRoomType(room.getRoomType());
				if (propertyFeeOrderTemplate.getRechargeWay() == 1) {
					feeOrder.setPayPlatform(2);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 2) {
					feeOrder.setPayPlatform(3);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 3) {
					feeOrder.setPayPlatform(4);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 4) {
					feeOrder.setPayPlatform(5);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 5) {
					feeOrder.setPayPlatform(6);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 6) {
					feeOrder.setPayPlatform(7);
				} else if (propertyFeeOrderTemplate.getRechargeWay() == 7) {
					feeOrder.setPayPlatform(8);
				}
				Estate estate = estateService.findById(room.getEstateId());
				if (estate != null) {
					feeOrder.setPropertyId(estate.getPropertyId());
					feeOrder.setEstateName(estate.getEstateName());
					Property property = propertyService.findById(estate.getPropertyId());
					if (property != null) {
						feeOrder.setPropertyName(property.getPropertyName());
					}
				}
				//创建套内账户消费账单
				EstateRoomAccountRecord record = new EstateRoomAccountRecord();
				record.setPropertyId(estate.getPropertyId());
				record.setEstateId(room.getEstateId());
				record.setSysUserId(sysUser.getUserId());
				record.setAccount(account);
				record.setRechargeWay(propertyFeeOrderTemplate.getRechargeWay());
				record.setRechargeOrUse(1L);
				record.setUsageAmount(feeOrder.getAccountFee());
				record.setRoomNo(room.getRoomNo());
				record.setRoomId(room.getId());
				estateRoomAccountRecordService.save(record);
				feeOrder.setAccountOrderId(record.getId());

				//修改小区账户余额
				room.setAccount(account);
				estateRoomService.updateById(room);
			}
			feeOrder.setRoomId(propertyFeeOrderTemplate.getRoomId());
		}
		feeOrder.setPlatform("web");
		feeOrder.setUserId(0);
		feeOrder.setNickName(sysUser.getNickName());


		if (propertyFeeOrderTemplate.getDiscount() != null) {
			if (propertyFeeOrderTemplate.getDiscount() != 10) {
				feeOrder.setWyfYhRate(BigDecimal.valueOf(propertyFeeOrderTemplate.getDiscount()));
			} else {
				feeOrder.setWyfYhRate(BigDecimal.valueOf(0.00));
			}
		} else {
			feeOrder.setWyfYhRate(BigDecimal.valueOf(0.00));
		}
		Date dates = new Date();
		Long dateInt = dates.getTime();
		String orderNo = "37UT1" + dateInt;
		feeOrder.setOrderNo(orderNo);
		feeOrder.setJiaYaYouhui(BigDecimal.valueOf(0.00));
		feeOrder.setCarYouhui(BigDecimal.valueOf(0.00));
		String beginDate = "2024-1-1";
		Date begin = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			begin = format.parse(beginDate);
		} catch (Exception e) {
			System.out.println("日期格式错误！");
		}
		String endDate = "2025-12-31";
		Date end = new Date();
		SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd");
		try {
			end = formats.parse(endDate);
		} catch (Exception e) {
			System.out.println("日期格式错误！");
		}
		feeOrder.setEndTime(end);
		feeOrder.setState(4);
		feeOrder.setDkRate(propertyFeeOrderTemplate.getDiscount());
		Date date1 = new Date();
		feeOrder.setPayTime(date1);
		feeOrder.setIsDel(0);
		QueryWrapper<PropertyFeeOrderTemplate> qw = new QueryWrapper<>();
		qw.eq("room_id", propertyFeeOrderTemplate.getRoomId());
		List<PropertyFeeOrderTemplate> templates = propertyFeeOrderTemplateService.list(qw);
		for (PropertyFeeOrderTemplate pt : templates) {
			if (pt.getFeeType().equals("wyf")) {
				feeOrder.setWyfSinglePrice(pt.getSinglePrice());
			}
			if (pt.getFeeType().equals("water")) {
				feeOrder.setWaterSinglePrice(pt.getSinglePrice());
			}
			if (pt.getFeeType().equals("eletrict")) {
				feeOrder.setElectricSinglePrice(pt.getSinglePrice());
			}
			if (pt.getFeeType().equals("car")) {
				feeOrder.setCarSinglePrice(pt.getSinglePrice());
			}
			if (pt.getFeeType().equals("rubbish")) {
				feeOrder.setTrashFee(pt.getSinglePrice());
			}
		}
		int count = 0;
		double wyf = 0.00;
		double wyfYh = 0.00;
		int wyfMonth = 0;
		double water = 0.00;
		int waterMonth = 0;
		double electric = 0.00;
		int electricMonth = 0;
		double car = 0.00;
		int carMonth = 0;
		double rubbish = 0.00;
		double rubbishYh = 0.00;
		int rubbishMonth = 0;
		double jiaya = 0.00;
		int jiayaMonth = 0;
		List<Integer> ids = propertyFeeOrderTemplate.getIds();
		if (CollUtil.isNotEmpty(ids)) {
			//循环缴费id清单
			for (Integer id : ids) {
				PropertyFeeOrderTemplate template = propertyFeeOrderTemplateService.findById(id);
				if (template != null) {
					if (template.getFeeType().equals("wyf")) {
						wyf += template.getTotalPrice().doubleValue();
						wyfYh += template.getYouhuiPrice().doubleValue();
						wyfMonth += 1;
					}
					if (template.getFeeType().equals("water")) {
						water += template.getTotalPrice().doubleValue();
						wyfMonth += 1;
					}
					if (template.getFeeType().equals("eletrict")) {
						electric += template.getTotalPrice().doubleValue();
						electricMonth += 1;
					}
					if (template.getFeeType().equals("car")) {
						car += template.getTotalPrice().doubleValue();
						carMonth += 1;
					}
					if (template.getFeeType().equals("rubbish")) {
						rubbish += template.getTotalPrice().doubleValue();
						rubbishYh += template.getYouhuiPrice().doubleValue();
						rubbishMonth += 1;
					}
					if (template.getFeeType().equals("jiaya")) {
						jiaya += template.getTotalPrice().doubleValue();
						jiayaMonth += 1;
					}
					if (propertyFeeOrderTemplate.getState() != null) {
						template.setState(propertyFeeOrderTemplate.getState());
						if (propertyFeeOrderTemplate.getState() == 2) {
							Date date = new Date();
							template.setPayTime(date);
						}
					}
					if (propertyFeeOrderTemplate.getRemark() != null) {
						template.setRemark(propertyFeeOrderTemplate.getRemark());
					}
					boolean bl = propertyFeeOrderTemplateService.updateById(template);
					if (bl) {
						count++;
					}
				}
			}
			if (propertyFeeOrderTemplate.getDiscount() != null) {
				feeOrder.setWyfFee(BigDecimal.valueOf(wyf * propertyFeeOrderTemplate.getDiscount() / 10));
				feeOrder.setWaterFee(BigDecimal.valueOf(water));
				feeOrder.setElectricFee(BigDecimal.valueOf(electric));
				feeOrder.setTrashFee(BigDecimal.valueOf(rubbish));
				feeOrder.setJiaYaFee(BigDecimal.valueOf(jiaya));
				feeOrder.setCarFee(BigDecimal.valueOf(car));
				feeOrder.setWyfYhPrice(BigDecimal.valueOf(wyfYh));
				feeOrder.setTrashYouhui(BigDecimal.valueOf(rubbishYh));
			}
			feeOrder.setWyfMonths(String.valueOf(wyfMonth));
			feeOrder.setWaterMonths(String.valueOf(waterMonth));
			feeOrder.setElectricMonths(String.valueOf(electricMonth));
			feeOrder.setCarMonths(String.valueOf(carMonth));
			feeOrder.setTotalMoney(BigDecimal.valueOf((wyf * propertyFeeOrderTemplate.getDiscount() / 10) + water + electric + rubbish + jiaya + car));
			if (feeOrder != null) {
				propertyFeeOrderService.save(feeOrder);
				//添加线下缴费详情
				for (Integer id : propertyFeeOrderTemplate.getIds()) {
					PropertyFeeOrderOfflineInfo info = new PropertyFeeOrderOfflineInfo();
					info.setOrderId(feeOrder.getId());
					info.setTemplateId(Long.valueOf(id));
					propertyFeeOrderOfflineInfoService.save(info);
				}
				//添加费用详情
				if (!feeOrder.getWyfFee().equals(BigDecimal.valueOf(0))) {
					PropertyFeeOrderInfo info = new PropertyFeeOrderInfo();
					info.setOrderId(feeOrder.getId());
					info.setUserId(0);
					info.setEstateId(feeOrder.getEstateId());
					info.setRoomId(feeOrder.getRoomId());
					info.setTotalPrice(feeOrder.getTotalMoney());
					info.setPayPrice(feeOrder.getPayMoney());
					info.setState(1);
					//添加日期
					String yearBeging = "";
					String yearEnd = "";
					String monthBeging = "";
					String monthEnd = "";
					String dateBeging = "01";
					String dateEnd = "28";
					List<PropertyFeeOrderTemplate> templateList = new ArrayList<>();
					List<PropertyFeeOrderTemplate> fujiaList = new ArrayList<>();
					//对id进行排序
					Collections.sort(propertyFeeOrderTemplate.getIds());
					for (Integer id : propertyFeeOrderTemplate.getIds()) {
						PropertyFeeOrderTemplate template = propertyFeeOrderTemplateService.findById(id);
						if (template.getFeeType().equals("wyf")) {
							templateList.add(template);
							info.setFeeType("wyf");
						}
						if (template.getFeeType().equals("car")) {
							templateList.add(template);
							info.setFeeType("car");
						}
						if (template.getFeeType().equals("rubbish")) {
							fujiaList.add(template);
						}
					}
					if (templateList.size() > 0) {
						yearBeging = templateList.get(0).getYearNum();
						yearEnd = templateList.get(templateList.size() - 1).getYearNum();
						monthBeging = templateList.get(0).getMonthNum();
						monthEnd = templateList.get(templateList.size() - 1).getMonthNum();
						info.setSinglePrice(templateList.get(0).getSinglePrice());
						info.setMonthCount(templateList.size());
					} else if (fujiaList.size() > 0) {
						yearBeging = fujiaList.get(0).getYearNum();
						yearEnd = fujiaList.get(fujiaList.size() - 1).getYearNum();
						monthBeging = fujiaList.get(0).getMonthNum();
						monthEnd = fujiaList.get(fujiaList.size() - 1).getMonthNum();
						info.setSinglePrice(fujiaList.get(0).getSinglePrice());
						info.setMonthCount(fujiaList.size());
					}
					if (!monthEnd.equals("10") && !monthEnd.equals("11") && !monthEnd.equals("12")) {
						monthEnd = "0" + monthEnd;
					}
					if (!monthBeging.equals("10") && !monthBeging.equals("11") && !monthBeging.equals("12")) {
						monthBeging = "0" + monthBeging;
					}
					String begingTime = yearBeging + "-" + monthBeging + "-" + dateBeging;
					String endTime = yearEnd + "-" + monthEnd + "-" + dateEnd;
					DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
					LocalDate BegingTime = LocalDate.parse(begingTime, formatter);
					LocalDate EndTime = LocalDate.parse(endTime, formatter);
					ZonedDateTime zonedDateTime1 = BegingTime.atStartOfDay(ZoneId.systemDefault());
					Date Date1 = Date.from(zonedDateTime1.toInstant());

					ZonedDateTime zonedDateTime2 = EndTime.atStartOfDay(ZoneId.systemDefault());
					Date Date2 = Date.from(zonedDateTime2.toInstant());

					info.setBeginDate(Date1);
					info.setEndDate(Date2);

					if (fujiaList.size() > 0) {
						info.setAttachSinglePrice(fujiaList.get(0).getSinglePrice());
						info.setAttachPrice(feeOrder.getTrashFee());
					}
					propertyFeeOrderInfoService.save(info);
				}
			}
		}
		return AjaxResult.success("成功修改" + count + "条数据");
	}


	/**
	 * 查询对应的年份列表
	 */
	@ApiOperation("查询物业费相关费用的模板列表")
	@PreAuthorize("@ss.hasPermi('module:feeOrderTemp:list')")
	@GetMapping("/yearList")
	public TableDataInfo<PropertyFeeOrderTemplate> yearList(@Validated PropertyFeeOrderTemplate propertyFeeOrderTemplate) {
		QueryWrapper<PropertyFeeOrderTemplate> qw = new QueryWrapper<>();
		if (propertyFeeOrderTemplate.getEstateId() != null) {
			qw.eq("estate_id", propertyFeeOrderTemplate.getEstateId());
		}
		if (propertyFeeOrderTemplate.getRoomId() != null) {
			qw.eq("room_id", propertyFeeOrderTemplate.getRoomId());
		}
		qw.eq("state", 0);
		qw.groupBy("year_num");
		qw.orderByDesc("year_num");
		TableDataInfo<PropertyFeeOrderTemplate> td = PageUtils.buildDataInfo(propertyFeeOrderTemplateService.page(PageUtils.buildPage(), qw));
		List<PropertyFeeOrderTemplate> list = td.getRows();
		return td;
	}
}
